package com.ruangong.GitClient.HunHePanAPI;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Test2 {

    public static String fetchFromApi(String query) {
        String responseContent = "";
        try {
            URL url = new URL("https://hunhepan.com/open/search/disk");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setDoOutput(true);

            String jsonInputString = "{\"q\": \"" + query + "\", \"page\": 1, \"size\": 10, \"time\": \"\", \"type\": \"\", \"exact\": true}";

            try(OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            try(BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                responseContent = response.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseContent;
    }

    public static List<MovieLink> getResult(String query) {
        List<MovieLink> movieLinks = new ArrayList<>();

        String apiResponse = Test2.fetchFromApi(query);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(apiResponse, JsonObject.class);
        if (jsonObject == null) return null;
        JsonObject dataObject = jsonObject.getAsJsonObject("data");

        // 检查 dataObject 是否为 null
        if (dataObject != null) {
            // 检查 listArray 是否存在且为有效的 JsonArray
            if (dataObject.has("list") && dataObject.get("list").isJsonArray()) {
                JsonArray listArray = dataObject.getAsJsonArray("list");

                // 遍历 listArray，获取 disk_type 和 link，并存入 MovieLink 对象列表
                for (JsonElement element : listArray) {
                    JsonObject item = element.getAsJsonObject();
                    String diskType = item.get("disk_type").getAsString();
                    String link = item.get("link").getAsString();
                    String diskName = item.get("disk_name").getAsString();
                    String shareUser = item.get("share_user").getAsString();
                    // 判断 is_mine 是否为 false
                    boolean isMine = item.get("is_mine").getAsBoolean();

                    //if (!isMine) {
                    //System.out.println(link);
                    MovieLink movieLink = new MovieLink(diskType, diskName, link, shareUser);
                    movieLinks.add(movieLink);
                    //}
                }
            } else {
                // 处理 listArray 不存在或不是有效的 JsonArray 的情况
                return null;
            }
        } else {
            // 处理 dataObject 为 null 的情况
            return null;
        }
        return movieLinks;
    }



//    public static void main(String[] args) {
////        String apiResponse = Test2.fetchFromApi("a");
////        System.out.println(apiResponse);
//        List<MovieLink> movieLinks = Test2.getResult("爱情公寓");
//        // 获取 movieLinks 列表
//
//        for (MovieLink link : movieLinks) {
//            System.out.println("Disk Type: " + link.getDiskType() + ", Link: " + link.getLink());
//        }
//    }
}
